use strict;
use warnings;
use Math::Calculus::Expression;
use Test::More tests => 3;

BEGIN {
    use_ok 'SPAN::Method::LinearEquation::Bissection';
}

my $exp = Math::Calculus::Expression->new;
$exp->addVariable('x');
$exp->setExpression('x^3 + (- 9*x ) + 3') or die $exp->getError;
diag("\nExpression: " . $exp->getExpression);

my $expected = 0.3369140625;
diag("Expected: x=" . $expected);

my $bissec   = SPAN::Method::LinearEquation::Bissection->new(
    expression => $exp,
    start      => 0,
    end        => 1,
    precision  => 0.001
);
my $got = $bissec->run;
diag("Got: x=" . $got);

cmp_ok( $got, '==', $expected, 'bissection run with expression: x^3 - 9*x + 3' );

$exp->setExpression('x^2 + x - 6') or die $exp->getError;
diag("\nExpression: " . $exp->getExpression);

$expected = 1.99999999999999;
diag("Expected: x=" . $expected);

$bissec->expression($exp);
$bissec->start(1); $bissec->end(3);
$bissec->precision(0.00000000000001);
$got = $bissec->run;
diag("Got: x=" . $got);

cmp_ok( sprintf("%.14f", $got), '==', $expected, 'bissection run with expression: x^2 + x - 6' );